//
//  selectCommunityController.m
//  QRMaster
//
//  Created by myApple on 15/6/4.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "selectCommunityController.h"
#import "OpertionPlist.h"
#import "communityModel.h"
#import "CommunitySelectTableViewCell.h"
#import "RequestNetWork.h"
#import "MJRefresh.h"
#import "PromptInfoView.h"
#import "checkPower.h"
#import "QRSingleton.h"
#import "CommunityListInfo.h"

@interface selectCommunityController ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView * CommunityNameTableView;
    NSMutableArray * CommunityInfoArray;
    QRSingleton * singleton;
}

@end

@implementation selectCommunityController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    self.title = NSLocalizedString(@"Filter", nil);
    CommunityInfoArray = [[NSMutableArray alloc]init];
    singleton = [QRSingleton sharedQRSingleton];
    
    [self prepareTableView];
    [self getDataFromContentFile];
    
    // Do any additional setup after loading the view.
}

-(void)prepareTableView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = [UIScreen mainScreen].bounds.size.height;
    float x = 0;
    float y = 0;
    float width = ScreenWidth;
    float height = ScreenHeight - 64;
    
    CommunityNameTableView = [[UITableView alloc]initWithFrame:CGRectMake(x, y, width, height) style:UITableViewStylePlain];
    CommunityNameTableView.delegate = self;
    CommunityNameTableView.dataSource = self;
    [self.view addSubview:CommunityNameTableView];
    CommunityNameTableView.tableFooterView = [[UIView alloc]init];
    
    __weak selectCommunityController * weakSelf = self;
    [CommunityNameTableView.mj_header beginRefreshing];
    CommunityNameTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf getCommunityData];
    }];
}

-(void)getDataFromContentFile
{
    [CommunityInfoArray removeAllObjects];
    NSMutableDictionary * data = [OpertionPlist readDataFromPlist];
    NSArray * communityData = [data objectForKey:COMMUNITYINFOFORKEY];
     CommunityListInfo * listInfo = [[CommunityListInfo alloc]init];
    for (NSDictionary * dict in communityData) {
        CommunityListModel * model = [[CommunityListModel alloc]init];
        model.communityName = [dict objectForKey:@"communityname"];
        model.communityId = [dict objectForKey:@"communityid"];
        model.role = [dict objectForKey:@"role"];
        model.areaCodeString = [dict objectForKey:@"areaCode"];
        [CommunityInfoArray addObject:model];
        [listInfo deleteCommunityListInfo:[dict objectForKey:@"communityid"]];
        [listInfo insertCommunityListInfo:[dict objectForKey:@"communityid"] withCommunityName:[dict objectForKey:@"communityname"] withCommunityAreaCode:[dict objectForKey:@"communityid"]];
    }
    [CommunityNameTableView reloadData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return CommunityInfoArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * string = @"cell";
    CommunitySelectTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
        cell = [[CommunitySelectTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    if (CommunityInfoArray.count > indexPath.row) {
        CommunityListModel * model = [CommunityInfoArray objectAtIndex:indexPath.row];
        cell.communiname = model.communityName;
        cell.communityid = model.communityId;
        cell.role = model.role;
        if ([model.communityId isEqualToString:_communityId]) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }else
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.communityBlock) {
        if ([self IsHaveNetwork] == NO ) {
            CommunityListModel * model = CommunityInfoArray[indexPath.row];
            [QRSingleton sharedQRSingleton].currentAreaCodeString = model.areaCodeString;
            singleton.communityId = model.communityId;
            singleton.communityName = model.communityName;
            [[NSUserDefaults standardUserDefaults] setObject:model.communityId forKey:@"communityId"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            self.communityBlock(model);
        }
        else
        {
            [PromptInfoView showMessage:NSLocalizedString(@"noNetWork", nil)];
        }

    }
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)getCommunityData
{
    if ([self IsHaveNetwork]) {
    [CommunityNameTableView.mj_header endRefreshing];
        return;
    }
    checkPower * power = [[checkPower alloc]init];
    [power getUserPower];
    power.powerBlock = ^(id data,id error)
    {
        if ([data objectForKey:@"data"] != nil)
        {
            [self getDataFromContentFile];
        }
        else
        {
            [PromptInfoView showMessage:[error objectForKey:@"info"]];
        }
        [CommunityNameTableView reloadData];
        [CommunityNameTableView.mj_header endRefreshing];
    };


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
